package com.javasm.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author：易鑫
 * @Version：1.0
 * @Date：2022/4/21-14:04
 * @Since:jdk1.8
 * @Description:
 */

/**
 * 公共字段自动填充
 */
@Component
public class MyMetaObjecthandler implements MetaObjectHandler {

    /**
     * 插入操作自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //BaseContext.getCurrentid():获取当前线程中已经存入的用户id
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentid());
        metaObject.setValue("updateUser",BaseContext.getCurrentid());
    }

    /**
     * 更新操作自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentid());
    }
}
